from ..models.QuanModels import *
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from ..models.BaseModels import *

def getUserInfo(request, id):
    _team = None
    _proxy = None
    _admin = None
    baidu = BaiduCode.objects.filter(id=id).first()
    user = Users.objects.using('app_db').filter(phone=baidu.mobile).first()
    if user:
        ptu = ProxyTeamUser.objects.using('app_db').filter(user=user).first()
        if ptu.proxy:
            proxy_user = ptu.proxy
        else:
            proxy_user = False
        if ptu.team:
            _team = customCreate(ptu.team.name, ptu.team.id, 2)
    else:
        proxy_user = Proxys.objects.using('app_db').filter(phone=baidu.mobile).first()
    if proxy_user:
        if proxy_user.proxy_type == 0:
            if  proxy_user.big_proxy_id == 0:
                bigProxy = Proxys.objects.using('app_db').filter(big_proxy_id=proxy_user.id).first()
                admin = Promotions.objects.using('app_db').filter(proxy=bigProxy).first()
                _proxy = customCreate(proxy_user.proxy_name, proxy_user.id, 3)
                _admin = customCreate(admin.admin_user.name, admin.admin_user.id, 5)
            else:
                admin = Promotions.objects.using('app_db').filter(proxy=proxy_user).first()
                _admin = customCreate(admin.admin_user.name, admin.admin_user.id, 5)
                proxy = Proxys.objects.using('app_db').filter(id=proxy_user.big_proxy_id).first()
                _proxy = customCreate(proxy.proxy_name, proxy.id, 3)
        elif proxy_user.proxy_type == 2:
            _proxy = customCreate(proxy_user.proxy_name, proxy_user.id, 4)
            proxy = Proxys.objects.using('app_db').filter(big_proxy_id=proxy_user.id).first()
            admin = Promotions.objects.using('app_db').filter(proxy_id=proxy.id).first()
            _admin = customCreate(admin.name, admin.id, 5)
        baidu.team = _team
        baidu.proxy = _proxy
        baidu.admin_user = _admin
        baidu.save()
        return HttpResponseRedirect('/admin/quanquaner/baiducode/')
    else:
        return HttpResponse('data error!')

def customCreate(name, custom_id, role):
    custom = Customs.objects.filter(custom_id=custom_id, role=role).first()
    if not custom:
        custom = Customs(name=name, custom_id=custom_id, role=role)
        custom.save()
    return custom

def QqOrderDetail(request):
    order = {
        'all': QqOrder.objects.using('app_db').filter(origin_type=1, task_status__in=[0,1,2]).count(),
        'yes': QqOrder.objects.using('app_db').filter(origin_type=1, task_status=0).count(),
        'no': QqOrder.objects.using('app_db').filter(origin_type=1, task_status=1).count(),
        'old': QqOrder.objects.using('app_db').filter(origin_type=1, task_status=2).count(),
    }
    order['yes_per'] = '{:.2%}'.format(order['yes']/order['all'])
    order['no_per'] = '{:.2%}'.format(order['no']/order['all'])
    return render_to_response('qq_order.html', {'order':order})

